package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.credit.*;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 人行征信白盒分拍平表
 *
 * @author guoqiang
 */

public class FundsCreditRecordParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();
    private final int feature = SerializerFeature.WRITE_MAP_NULL_FEATURES;

    /**
     * 人行征信白盒分拍平表
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        FundsCreditRecord obj = (FundsCreditRecord) model;
        FundsCreditData data = obj.getData() == null ? new FundsCreditData() : obj.getData();
        StringBuilder sb = getBaseInfo(obj, successful);
        sb.append(formatValue(obj.getLoanId())).append(CRT001);
        sb.append(formatValue(obj.getChannel())).append(CRT001);
        sb.append(formatValue(obj.getIsReject())).append(CRT001);
        sb.append(formatValue(data.getResult())).append(CRT001);
        sb.append(formatValue(data.getReason())).append(CRT001);
        sb.append(formatValue(data.getChannel())).append(CRT001);

        CreditScore creditObj = data.getCreditScore();
        StringBuilder TxSb = new StringBuilder(500);
        if (creditObj == null){
            TxSb.delete(0, TxSb.length());
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append("rejectReason").append(CRT001);
            TxSb.append(formatValue(data.getFunderRejectSubject())).append(CRT001);
            TxSb.append(formatValue(data.getAllFunderSubject())).append(CRT001);
            TxSb.append(data.getRejectReason() == null ? "" : JSON.toJSONString(data.getRejectReason(), feature)).append(CRT001);
            key.set(getType() + getSubType());
            val.set((sb.toString() + TxSb).replace("\n", " ").replace("\r", " "));
            context.write(key, val);
            return true;
        }
        XzTrustScore xzTrust = creditObj.getXzTrustScore();
        if(xzTrust != null) {
            TrustScoreData xzTrustData = xzTrust.getData();
            TxSb.delete(0, TxSb.length());
            TxSb.append(CRT001);
            TxSb.append(formatValue(xzTrust.getPid())).append(CRT001);
            TxSb.append(formatValue(xzTrust.getStatus())).append(CRT001);
            TxSb.append(formatValue(xzTrustData.getYxGrade1().getScore())).append(CRT001);
            TxSb.append(formatValue(xzTrustData.getYxGrade1().getCode())).append(CRT001);
            TxSb.append(formatValue(xzTrustData.getYxGrade2().getScore())).append(CRT001);
            TxSb.append(formatValue(xzTrustData.getYxGrade2().getCode())).append(CRT001);
            TxSb.append(formatValue(xzTrustData.getYxGrade3().getScore())).append(CRT001);
            TxSb.append(formatValue(xzTrustData.getYxGrade3().getCode())).append(CRT001);
            TxSb.append(formatValue(xzTrustData.getYxGrade4().getScore())).append(CRT001);
            TxSb.append(formatValue(xzTrustData.getYxGrade4().getCode())).append(CRT001);
            TxSb.append(formatValue(xzTrustData.getYxGrade5().getScore())).append(CRT001);
            TxSb.append(formatValue(xzTrustData.getYxGrade5().getCode())).append(CRT001);
            TxSb.append(formatValue(xzTrustData.getYxGrade6().getScore())).append(CRT001);
            TxSb.append(formatValue(xzTrustData.getYxGrade6().getCode())).append(CRT001);
            TxSb.append(formatValue(xzTrustData.getYxGrade7().getScore())).append(CRT001);
            TxSb.append(formatValue(xzTrustData.getYxGrade7().getCode())).append(CRT001);
            TxSb.append(formatValue(xzTrustData.getYxGrade8().getScore())).append(CRT001);
            TxSb.append(formatValue(xzTrustData.getYxGrade8().getCode())).append(CRT001);
            TxSb.append(formatValue(xzTrustData.getYxGrade9().getScore())).append(CRT001);
            TxSb.append(formatValue(xzTrustData.getYxGrade9().getCode())).append(CRT001);
            TxSb.append(formatValue(xzTrustData.getYxGrade10().getScore())).append(CRT001);
            TxSb.append(formatValue(xzTrustData.getYxGrade10().getCode())).append(CRT001);
            TxSb.append(formatValue(xzTrustData.getYxGrade11().getScore())).append(CRT001);
            TxSb.append(formatValue(xzTrustData.getYxGrade11().getCode())).append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append("xzTrustScore").append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            key.set(getType() + getSubType());
            val.set((sb.toString() + TxSb).replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }
        LjTrustScore ljTrust = creditObj.getLjTrustScore();
        if(ljTrust != null) {
            LjTrustScoreData ljTrustData = ljTrust.getData();
            TxSb.delete(0, TxSb.length());
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(formatValue(ljTrustData.getMl0001().getScore())).append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(formatValue(ljTrustData.getMl0002().getScore())).append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(formatValue(ljTrustData.getMl0003().getScore())).append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(formatValue(ljTrustData.getMl0004().getScore())).append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(formatValue(ljTrustData.getMl0005().getScore())).append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(formatValue(ljTrustData.getMl0006().getScore())).append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(formatValue(ljTrustData.getMl0007().getScore())).append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(formatValue(ljTrustData.getMl0008().getScore())).append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(formatValue(ljTrustData.getMl0009().getScore())).append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(formatValue(ljTrustData.getMl0010().getScore())).append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(formatValue(ljTrustData.getMl0011().getScore())).append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append("ljTrustScore").append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            key.set(getType() + getSubType());
            val.set((sb.toString() + TxSb).replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }
        UniversalTrustScore trust = creditObj.getTrustScore();
        if(trust != null) {
            TxSb.delete(0, TxSb.length());
            TxSb.append(formatValue(trust.getReqId())).append(CRT001);
            TxSb.append(formatValue(trust.getPid())).append(CRT001);
            TxSb.append(formatValue(trust.getStatus())).append(CRT001);
            TrustScoreData trustData = trust.getData();
            TxSb.append(formatValue(trustData.getYxGrade1().getScore())).append(CRT001);
            TxSb.append(formatValue(trustData.getYxGrade1().getCode())).append(CRT001);
            TxSb.append(formatValue(trustData.getYxGrade2().getScore())).append(CRT001);
            TxSb.append(formatValue(trustData.getYxGrade2().getCode())).append(CRT001);
            TxSb.append(formatValue(trustData.getYxGrade3().getScore())).append(CRT001);
            TxSb.append(formatValue(trustData.getYxGrade3().getCode())).append(CRT001);
            TxSb.append(formatValue(trustData.getYxGrade4().getScore())).append(CRT001);
            TxSb.append(formatValue(trustData.getYxGrade4().getCode())).append(CRT001);
            TxSb.append(formatValue(trustData.getYxGrade5().getScore())).append(CRT001);
            TxSb.append(formatValue(trustData.getYxGrade5().getCode())).append(CRT001);
            TxSb.append(formatValue(trustData.getYxGrade6().getScore())).append(CRT001);
            TxSb.append(formatValue(trustData.getYxGrade6().getCode())).append(CRT001);
            TxSb.append(formatValue(trustData.getYxGrade7().getScore())).append(CRT001);
            TxSb.append(formatValue(trustData.getYxGrade7().getCode())).append(CRT001);
            TxSb.append(formatValue(trustData.getYxGrade8().getScore())).append(CRT001);
            TxSb.append(formatValue(trustData.getYxGrade8().getCode())).append(CRT001);
            TxSb.append(formatValue(trustData.getYxGrade9().getScore())).append(CRT001);
            TxSb.append(formatValue(trustData.getYxGrade9().getCode())).append(CRT001);
            TxSb.append(formatValue(trustData.getYxGrade10().getScore())).append(CRT001);
            TxSb.append(formatValue(trustData.getYxGrade10().getCode())).append(CRT001);
            TxSb.append(formatValue(trustData.getYxGrade11().getScore())).append(CRT001);
            TxSb.append(formatValue(trustData.getYxGrade11().getCode())).append(CRT001);
            TrustYxGrade grade12 = trustData.getYxGrade12();
            TxSb.append(formatValue(grade12 == null ? "" : grade12.getScore())).append(CRT001);
            TxSb.append(formatValue(grade12 == null ? "" : grade12.getCode())).append(CRT001);
            TxSb.append("UniversalTrustScore").append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            key.set(getType() + getSubType());
            val.set((sb.toString() + TxSb).replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }
        XinWangCreditScore xinWang = creditObj.getXinWangCreditScore();
        if(xinWang != null) {
            TxSb.delete(0, TxSb.length());
            TxSb.append(CRT001);
            TxSb.append(formatValue(xinWang.getPid())).append(CRT001);
            TxSb.append(formatValue(xinWang.getStatus())).append(CRT001);
            XinWangCreditScoreData xinWangData = xinWang.getData();
            TxSb.append(formatValue(xinWangData.getYxGrade1().getScore())).append(CRT001);
            TxSb.append(formatValue(xinWangData.getYxGrade1().getCode())).append(CRT001);
            TxSb.append(formatValue(xinWangData.getYxGrade2().getScore())).append(CRT001);
            TxSb.append(formatValue(xinWangData.getYxGrade2().getCode())).append(CRT001);
            TxSb.append(formatValue(xinWangData.getYxGrade3().getScore())).append(CRT001);
            TxSb.append(formatValue(xinWangData.getYxGrade3().getCode())).append(CRT001);
            TxSb.append(formatValue(xinWangData.getYxGrade4().getScore())).append(CRT001);
            TxSb.append(formatValue(xinWangData.getYxGrade4().getCode())).append(CRT001);
            TxSb.append(formatValue(xinWangData.getYxGrade5().getScore())).append(CRT001);
            TxSb.append(formatValue(xinWangData.getYxGrade5().getCode())).append(CRT001);
            TxSb.append(formatValue(xinWangData.getYxGrade6().getScore())).append(CRT001);
            TxSb.append(formatValue(xinWangData.getYxGrade6().getCode())).append(CRT001);
            TxSb.append(formatValue(xinWangData.getYxGrade7().getScore())).append(CRT001);
            TxSb.append(formatValue(xinWangData.getYxGrade7().getCode())).append(CRT001);
            TxSb.append(formatValue(xinWangData.getYxGrade8().getScore())).append(CRT001);
            TxSb.append(formatValue(xinWangData.getYxGrade8().getCode())).append(CRT001);
            TxSb.append(formatValue(xinWangData.getYxGrade9().getScore())).append(CRT001);
            TxSb.append(formatValue(xinWangData.getYxGrade9().getCode())).append(CRT001);
            TxSb.append(formatValue(xinWangData.getYxGrade10().getScore())).append(CRT001);
            TxSb.append(formatValue(xinWangData.getYxGrade10().getCode())).append(CRT001);
            TxSb.append(formatValue(xinWangData.getYxGrade11().getScore())).append(CRT001);
            TxSb.append(formatValue(xinWangData.getYxGrade11().getCode())).append(CRT001);
            TxSb.append(formatValue(xinWangData.getYxGrade12().getScore())).append(CRT001);
            TxSb.append(formatValue(xinWangData.getYxGrade12().getCode())).append(CRT001);
            TxSb.append("xinWangCreditScore").append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            key.set(getType() + getSubType());
            val.set((sb.toString() + TxSb).replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }
        HeiKaCreditScore heika = creditObj.getHeiKaCreditScore();
        if(heika != null) {
            TxSb.delete(0, TxSb.length());
            TxSb.append(formatValue(heika.getReqId())).append(CRT001);
            TxSb.append(formatValue(heika.getPid())).append(CRT001);
            TxSb.append(formatValue(heika.getStatus())).append(CRT001);
            HeiKaCreditScoreData heikaData = heika.getData();
            TxSb.append(formatValue(heikaData.getYxGrade1().getScore())).append(CRT001);
            TxSb.append(formatValue(heikaData.getYxGrade1().getCode())).append(CRT001);
            TxSb.append(formatValue(heikaData.getYxGrade2().getScore())).append(CRT001);
            TxSb.append(formatValue(heikaData.getYxGrade2().getCode())).append(CRT001);
            TxSb.append(formatValue(heikaData.getYxGrade3().getScore())).append(CRT001);
            TxSb.append(formatValue(heikaData.getYxGrade3().getCode())).append(CRT001);
            TxSb.append(formatValue(heikaData.getYxGrade4().getScore())).append(CRT001);
            TxSb.append(formatValue(heikaData.getYxGrade4().getCode())).append(CRT001);
            TxSb.append(formatValue(heikaData.getYxGrade5().getScore())).append(CRT001);
            TxSb.append(formatValue(heikaData.getYxGrade5().getCode())).append(CRT001);
            TxSb.append(formatValue(heikaData.getYxGrade6().getScore())).append(CRT001);
            TxSb.append(formatValue(heikaData.getYxGrade6().getCode())).append(CRT001);
            TxSb.append(formatValue(heikaData.getYxGrade7().getScore())).append(CRT001);
            TxSb.append(formatValue(heikaData.getYxGrade7().getCode())).append(CRT001);
            TxSb.append(formatValue(heikaData.getYxGrade8().getScore())).append(CRT001);
            TxSb.append(formatValue(heikaData.getYxGrade8().getCode())).append(CRT001);
            TxSb.append(formatValue(heikaData.getYxGrade9().getScore())).append(CRT001);
            TxSb.append(formatValue(heikaData.getYxGrade9().getCode())).append(CRT001);
            TxSb.append(formatValue(heikaData.getYxGrade10().getScore())).append(CRT001);
            TxSb.append(formatValue(heikaData.getYxGrade10().getCode())).append(CRT001);
            TxSb.append(formatValue(heikaData.getYxGrade11().getScore())).append(CRT001);
            TxSb.append(formatValue(heikaData.getYxGrade11().getCode())).append(CRT001);
            TxSb.append(formatValue(heikaData.getYxGrade12().getScore())).append(CRT001);
            TxSb.append(formatValue(heikaData.getYxGrade12().getCode())).append(CRT001);
            TxSb.append("heiKaCreditScore").append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            key.set(getType() + getSubType());
            val.set((sb.toString() + TxSb).replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_funds_credit_record_inc";
    }

    @Override
    public String getSubType() {
        return ".main";
    }

    @Override
    public String getType() {
        return RecordType.FUNDS_CREDIT_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "人行征信白盒分拍平表";
    }
}
